package thread.ch02;

import java.io.IOException;
import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import thread.annotation.ThreadSafe;

/**
 * 利用像AtomicLong这样已有的线程安全对象管理类的状态是非常实用的，相比于非线程安全对象，判断一个线程安全对象的可能状态和状态的转换要容易的多。
 * 这简化了维护和验证线程安全性的工作
 * <p />
 * CountingFactorizer将它的线程安全性委托给了AtomicLong
 * 
 * @author Administrator
 * 
 */
@ThreadSafe
public class CountingFactorizer extends HttpServlet {

	private final AtomicLong count = new AtomicLong(0);

	public long getCount() {
		return count.get();
	}

	@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		BigInteger i = extractFromRequest(request);
		BigInteger[] factors = factor(i);
		count.incrementAndGet();
		encodeIntoResponse(response, factors);
	}

	private void encodeIntoResponse(ServletResponse response,
			BigInteger[] factors) {
		// TODO Auto-generated method stub

	}

	private BigInteger[] factor(BigInteger i) {
		// TODO Auto-generated method stub
		return null;
	}

	private BigInteger extractFromRequest(ServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}
}
